import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class RefreshDemoPage3 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return RefreshDemoPage3State();
  }
}

class RefreshDemoPage3State extends State<RefreshDemoPage3> {
  // 监听滚动
  ScrollController scrollController = ScrollController();

  List<String> dataList = new List();

  final int dataSize = 30;

  bool flag = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 初次进入的时候显示刷新按钮
    Future.delayed(Duration(milliseconds: 500), () {
      scrollController.position.animateTo(-141,
          duration: Duration(milliseconds: 600), curve: Curves.linear);
    });
  }

  Future onRefresh() async {
    Future.delayed(Duration(seconds: 2));
    for (int i = 0; i < dataSize; i++) {
      dataList.add("refresh");
    }
    if (flag) {
      return;
    }
    setState(() {});
  }

  Future loadMore() async {
    Future.delayed(Duration(seconds: 2));
    for (int i = 0; i < dataSize; i++) {
      dataList.add("loadMore");
    }
    if (flag) {
      return;
    }
    setState(() {});
  }

  @override
  void dispose() {
    flag = true;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('下拉刷新2')),
      body: NotificationListener(
        onNotification: (ScrollNotification notification) {
          if (notification is ScrollNotification) {
            if (scrollController.position.pixels > 0 &&
                scrollController.position.pixels ==
                    scrollController.position.maxScrollExtent) {
              loadMore();
            }
          }
          return false;
        },
        child: CustomScrollView(
          controller: scrollController,

          ///回弹效果
          physics: const BouncingScrollPhysics(
              parent: AlwaysScrollableScrollPhysics()),
          slivers: <Widget>[
            // 下拉刷新的效果
            CupertinoSliverRefreshControl(
              refreshIndicatorExtent: 100,
              refreshTriggerPullDistance: 140,
              onRefresh: onRefresh,
            ),
            SliverSafeArea(
              sliver: SliverList(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    if (dataList.length == index) {
                      return Container(
                        margin: EdgeInsets.all(10),
                        child: Align(
                          child: CircularProgressIndicator(),
                        ),
                      );
                    }
                    return Card(
                      child: Container(
                        height: 60,
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 20),
                        child: Text(
                            'item ${dataList.length > 0 ? dataList[index] : ""} $index'),
                      ),
                    );
                  },
                  childCount: (dataList.length >= dataSize)
                      ? dataList.length + 1
                      : dataList.length,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
